package cn.edu.tsinghua.cic.ccss.qo;

import java.util.Arrays;
import java.util.Date;

import org.forten.core.dao.rdb.hibernate.QueryInfo;
import org.forten.core.dao.rdb.query.Operator;
import org.forten.core.dao.rdb.query.hql.expression.Compare;
import org.forten.core.dao.rdb.query.hql.expression.Expression;
import org.forten.core.dao.rdb.query.parameter.Param;
import org.forten.core.dao.rdb.query.parameter.Type;
import org.forten.core.qo.QueryEntity;
import org.forten.core.qo.QueryField;
import org.forten.core.qo.QueryField.MatchType;
import org.forten.core.qo.QueryObject;
import org.forten.utils.common.DateUtil;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import cn.edu.tsinghua.cic.ccss.entity.Course;

@QueryEntity(Course.class)
@Component("courseSelectCadreQo")
@Scope("prototype")
public class CourseSelectCadreQo implements QueryObject{
	@QueryField(fieldName = "name", paramName = "name", operator = Operator.LIKE , matchMode = MatchType.ANYWHERE)
	private String name;
	@QueryField(fieldName ="teacherName", paramName = "teacherName", operator = Operator.LIKE, matchMode = MatchType.ANYWHERE)
	private String teacherName;
	@QueryField(fieldName ="teachTime",paramName ={"start","end"}, operator = Operator.BETWEEN, type = Type.DATE)
	private Date[] teachTimePeriod;
	private String start;
	private String end;
	
	@Override
	public void clean() {
		teacherName = null;
		name = null;
		start = null;
		end = null;
	}

	@Override
	public void beforeBuildQueryInfo() {
		//若起始时间不为null，并且结束时间为null，则查询当前系统时间的所有的课程
				if(start !=null && "".equals(end) && !"".equals(start)){
					teachTimePeriod = new Date[2];
					teachTimePeriod[0] = DateUtil.convertStringToDate(start, "yyyyMMdd");
					Date currDate = DateUtil.currentTime();
					end = DateUtil.convertDateToString(currDate, "yyyyMMdd");
					teachTimePeriod[1] = DateUtil.convertStringToDate(end, "yyyyMMdd");
				} else 
				//若起始时间和结束时间均为null
				if(start != null && end != null && !"".equals(start) && !"".equals(end)){
					teachTimePeriod = new Date[2];
					teachTimePeriod[0] = DateUtil.convertStringToDate(start, "yyyyMMdd");
					teachTimePeriod[1] = DateUtil.convertStringToDate(end, "yyyyMMdd");
				}
				//不符合规则的全取
				 else {
					teachTimePeriod = null;
				}
		
	}

	@Override
	public QueryInfo afterBuildQueryInfo(QueryInfo qi) {
//		String hql = "FROM Course WHERE to_date(:currTime,'yyyy/MM/dd HH24:MI') BETWEEN startTimeChoose AND teachTime ";
		
		Date currentTime = DateUtil.currentTime();
		Expression leBegin = Compare.lt("startTimeChoose",
		Param.getCommonParam("begin", currentTime, Type.DATE));
		Expression geEnd = Compare.gt("endTimeChoose",
		Param.getCommonParam("over", currentTime, Type.DATE));
		qi.addExpressionsToWhere(leBegin, geEnd);
//		qi.setOrderby(OrderBy.getOrderBy(Order.desc("teachTime")));
		return qi;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getTeacherName() {
		return teacherName;
	}

	public void setTeacherName(String teacherName) {
		this.teacherName = teacherName;
	}

	public String getStart() {
		return start;
	}

	public void setStart(String start) {
		this.start = start;
	}

	public String getEnd() {
		return end;
	}

	public void setEnd(String end) {
		this.end = end;
	}

	public Date[] getTeachTimePeriod() {
		return teachTimePeriod;
	}

	public void setTeachTimePeriod(Date[] teachTimePeriod) {
		this.teachTimePeriod = teachTimePeriod;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((end == null) ? 0 : end.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((start == null) ? 0 : start.hashCode());
		result = prime * result + Arrays.hashCode(teachTimePeriod);
		result = prime * result
				+ ((teacherName == null) ? 0 : teacherName.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		CourseSelectCadreQo other = (CourseSelectCadreQo) obj;
		if (end == null) {
			if (other.end != null)
				return false;
		} else if (!end.equals(other.end))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (start == null) {
			if (other.start != null)
				return false;
		} else if (!start.equals(other.start))
			return false;
		if (!Arrays.equals(teachTimePeriod, other.teachTimePeriod))
			return false;
		if (teacherName == null) {
			if (other.teacherName != null)
				return false;
		} else if (!teacherName.equals(other.teacherName))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "CourseSelectCadreQo [name=" + name + ", teacherName="
				+ teacherName + ", teachTimePeriod="
				+ Arrays.toString(teachTimePeriod) + ", start=" + start
				+ ", end=" + end + "]";
	}



}
